package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.DuplicateStrategy;
import com.mp.approvalreq.entity.dto.GlobalSettingDTO;
import com.mp.approvalreq.service.SelectApprovalServicePrx;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.message.Message;
import com.plusmoney.message.SuccessMsg;
import com.plusmoney.util.ParameterUtils;
import com.plusmoney.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/updateGlobalSetting")
public class UpdateGlobalSetting extends AbstractServlet {
    private static final Logger logger = LoggerFactory.getLogger(UpdateGlobalSetting.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);
            SelectApprovalServicePrx selectApprovalService = ServiceUtils.getSelectApprovalServicePrx();

            String reqBody = getContent(req);
            logger.info("保存全局设置:{}", reqBody);
            GlobalSettingDTO globalSetting = JSONObject.parseObject(reqBody, GlobalSettingDTO.class);
            globalSetting.setCompanyId(user.getCompanyId());

            selectApprovalService.updateGlobalSetting(globalSetting);

            Message.printJson(resp, SuccessMsg.getDefaultMsg());
        } catch (Throwable throwable) {
            onThrowable(req, resp, throwable);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);

            SelectApprovalServicePrx selectApprovalService = ServiceUtils.getSelectApprovalServicePrx();
            GlobalSettingDTO globalSetting = selectApprovalService.getGlobalSetting(user.getCompanyId());


            Integer duplicateStrategy = ParameterUtils.getIntegerParameter(req, "duplicateStrategy", globalSetting.getDuplicateStrategy());
            String financialFlow = ParameterUtils.getStringParameter(req, "financialFlow", false);
            if (Utils.isNull(financialFlow)) {
                financialFlow = globalSetting.getFinancialFlow();
            }
            logger.info("json:{}", duplicateStrategy);
            check(duplicateStrategy);

            GlobalSettingDTO getGlobalSettingDTO = initGlobalSettingDTO(user.getCompanyId(), duplicateStrategy, financialFlow);

            selectApprovalService.updateGlobalSetting(getGlobalSettingDTO);

            Message.printJson(resp, SuccessMsg.getDefaultMsg());
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    private GlobalSettingDTO initGlobalSettingDTO(Integer companyId, Integer duplicateStrategy, String financialFlow) {
        GlobalSettingDTO globalSettingDTO = new GlobalSettingDTO();
        globalSettingDTO.setCompanyId(companyId);
        globalSettingDTO.setDuplicateStrategy(duplicateStrategy);
        globalSettingDTO.setFinancialFlow(financialFlow);
        return globalSettingDTO;
    }

    private void check(Integer duplicateStrategy) {
        if (Utils.isNull(duplicateStrategy)) {
            throw new RpcServerLogicalException(1, "去重策略不能为空");
        }

        if (duplicateStrategy < DuplicateStrategy.REMAIN_LAST_ITEM
                || duplicateStrategy > DuplicateStrategy.NO_REMOVE) {
            throw new RpcServerLogicalException(1, "未知的去重策略");
        }
    }
}
